<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/27
 * Time: 15:18
 */

namespace app\api\controller;


use think\Db;

class My extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 基本信息
     * @return \think\response\Json
     * range     调用场景   1 我的    2 编辑
     */
    public function userInfo($range){
        $userinfo = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_image, user_estate, user_role, user_topic')
            ->find();
        if($range == 1){
            $message_num = $this->myMessage(2); // 留言数
            $topic_num = $this->myTopic(2); // 话题数
            $collect_num = $this->myCollect(2); // 我的收藏
            $repair_num = $this->myRepair(2);
            $userinfo['message_num'] = $message_num;
            $userinfo['topic_num'] = $topic_num;
            $userinfo['collect_num'] = $collect_num;
            $userinfo['repair_num'] = $repair_num;
        }
        return json(['status' => 200, 'msg' => $userinfo]);
    }
    
    /**
     * 编辑个人信息
     * @return \think\response\Json
     * user_username     昵称
     * user_image        头像
     */
    public function userUpdate(){
        $user_username = input('user_username');
        $user_image = input('user_image');
        $update = [
            'user_username' => $user_username,
            'user_image' => $user_image
        ];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '保存成功']);
        }else{
            return json(['status' => 0, 'msg' => '保存失败']);
        }
    }
    
    /**
     * 我的留言列表
     * @return \think\response\Json
     */
    public function messageList(){
        $list = $this->myMessage(1);
        if($list){
            foreach($list as $k => $v){
                $list[$k]['topic_message_time'] = date('Y-m-d H:i', strtotime($v['topic_message_time']));
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的话题列表
     * @return \think\response\Json
     */
    public function topicList(){
        $list = $this->myTopic(1);
        if($list){
            foreach($list as $k => $v){
                $list[$k]['topic_fb_time'] = date('Y-m-d H:i', strtotime($v['topic_fb_time']));
                $list[$k]['topic_img'] = explode(',', trim($v['topic_img'], ','));
                // 收藏数量
                $collect_num = Db::table('up_topic_like')
                    ->where('topic_id', $v['topic_id'])
                    ->count();
                // 点赞数量
                $dz_num = Db::table('up_topic_dz')
                    ->where('topic_id', $v['topic_id'])
                    ->count();
                // 评论数量
                $comment_num = Db::table('up_topic_message')
                    ->where('topic_id', $v['topic_id'])
                    ->count();
                // 是否点赞
                $is_dz = Db::table('up_topic_dz')
                    ->where('topic_dz_user', $this->user_id)
                    ->where('topic_id', $v['topic_id'])
                    ->find();
                // 是否收藏
                $is_collect = Db::table('up_topic_like')
                    ->where('topic_like_user', $this->user_id)
                    ->where('topic_id', $v['topic_id'])
                    ->find();
                $list[$k]['dz_num'] = $dz_num;
                $list[$k]['collect_num'] = $collect_num;
                $list[$k]['comment_num'] = $comment_num;
                if($is_dz){
                    $list[$k]['is_dz'] = 1;
                }else{
                    $list[$k]['is_dz'] = 2;
                }
                if($is_collect){
                    $list[$k]['is_collect'] = 1;
                }else{
                    $list[$k]['is_collect'] = 2;
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的收藏
     * @return \think\response\Json
     * page   页码
     */
    public function collectList(){
        $list = $this->myCollect(1);
        if($list){
            foreach($list as $k => $v){
                $list[$k]['topic_fb_time'] = date('Y-m-d H:i', strtotime($v['topic_fb_time']));
                $list[$k]['topic_img'] = explode(',', trim($v['topic_img'], ','));
                // 收藏数量
                $collect_num = Db::table('up_topic_like')
                    ->where('topic_id', $v['topic_id'])
                    ->count();
                // 点赞数量
                $dz_num = Db::table('up_topic_dz')
                    ->where('topic_id', $v['topic_id'])
                    ->count();
                // 评论数量
                $comment_num = Db::table('up_topic_message')
                    ->where('topic_id', $v['topic_id'])
                    ->count();
                // 是否点赞
                $is_dz = Db::table('up_topic_dz')
                    ->where('topic_dz_user', $this->user_id)
                    ->where('topic_id', $v['topic_id'])
                    ->find();
                // 是否收藏
                $is_collect = Db::table('up_topic_like')
                    ->where('topic_like_user', $this->user_id)
                    ->where('topic_id', $v['topic_id'])
                    ->find();
                $list[$k]['dz_num'] = $dz_num;
                $list[$k]['collect_num'] = $collect_num;
                $list[$k]['comment_num'] = $comment_num;
                if($is_dz){
                    $list[$k]['is_dz'] = 1;
                }else{
                    $list[$k]['is_dz'] = 2;
                }
                if($is_collect){
                    $list[$k]['is_collect'] = 1;
                }else{
                    $list[$k]['is_collect'] = 2;
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 编辑话题
     * @return \think\response\Json
     * topic_id    话题ID
     * topic_type_id    话题类型
     * xq_id        小区ID
     * topic_title    话题标题
     * topic_content   话题内容
     * topic_img     话题图片
     */
    public function updateTopic(){
        $topic_id = input('topic_id');
        $topic_type = input('topic_type_id');
        $topic_xq = input('xq_id');
        $topic_title = trim(input('topic_title'), ' ');
        $topic_content = trim(input('topic_content'), ' ');
        $topic_img = input('topic_img');
    
        $update['topic_type'] = $topic_type;
        $update['topic_xq'] = $topic_xq;
        $update['topic_title'] = $topic_title;
        $update['topic_content'] = $topic_content;
        $update['topic_img'] = $topic_img;
        $res = Db::table('up_topic')->where('topic_id', $topic_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' =>"编辑成功"]);
        }else{
            return json(['status' => 200, 'msg' =>"编辑失败"]);
        }
    }
    
    /**
     * 我的上报列表
     * @return \think\response\Json
     * page   页码
     * xq_id    小区ID
     */
    public function myRepairList(){
        $where['repair_user_id'] = $this->user_id;
        $xq_id = input('xq_id');
        if(isset($xq_id) && !empty($xq_id)){
            $where['repair_plot_id'] = $xq_id;
        }
        $list = Db::table('up_repair')
            ->join('up_repair_type', 'rt_id = repair_type_id', 'left')
            ->field('repair_id, rt_name, repair_explain, repair_time, repair_status')
            ->where($where)
            ->order('repair_time desc')
            ->page($this->page, 20)
            ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['repair_time'] = date('Y-m-d H:i', strtotime($v['repair_time']));
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的缴费记录
     * @param $pay_cate
     * @return \think\response\Json
     * pay_cate    缴费类型   0 全部   1 水电费  2 物业费   3 停车费
     * page    页码
     */
    public function myPay($pay_cate){
        $where['pay_user_id'] = $this->user_id;
        $where['pay_status'] = 2;
        if($pay_cate == 0){
            $where['pay_cate'] = ['IN', '1,2,3,4'];
        }elseif($pay_cate == 1){
            $where['pay_cate'] = ['IN', '1,2'];
        }elseif($pay_cate == 2){
            $where['pay_cate'] = 3;
        }else{
            $where['pay_cate'] = 4;
        }
        $list = Db::table('up_pay')
            ->join('up_u_user', 'pay_user_id = user_id', 'left')
            ->field('pay_id, pay_xq, pay_lou, pay_house, pay_cate, pay_price, pay_time, user_username')
            ->where($where)
            ->order('pay_time desc')
            ->page($this->page, 20)
            ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['pay_time'] = date('Y-m-d H:i', strtotime($v['pay_time']));
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 验证密码
     * @param $pwd    密码
     * @return \think\response\Json
     */
    public function checkPwd($pwd){
        $md5_pwd = md5(trim($pwd, ' '));
        $password = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_password');
        if($md5_pwd != $password){
            return json(['status' => 0, 'msg' => '密码错误']);
        }else{
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 更换手机号
     * @return \think\response\Json
     * phone  新的手机号
     * code   验证码
     */
    public function bandPhone(){
        $phone = input('phone');
        $code = input('code');
        $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if($user){
            return json(['status' => 0, 'msg' => '该手机号已注册']);
        }
        //=========验证验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码超时']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码失效']);
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_phone', $phone);
        if($res){
            $user = Db::table('up_u_user')->where('user_id', $this->user_id)->find();
            return json(['status' => 200, 'msg' => $user]);
        }else{
            return json(['status' => 0, 'msg' => '绑定失败']);
        }
    }
    
    /**
     * 重置密码
     * @return \think\response\Json
     * phone    手机号
     * pwd      密码
     * code     验证码
     */
    public function resetPassword(){
        $phone = trim(input('phone'), ' ');
        $pwd = trim(input('pwd'), ' ');
        $code = trim(input('code'), ' ');
        if($code == '') return json(['status' => 0, 'msg' => '请填写验证码']);
        if($pwd == '') return json(['status' => 0, 'msg' => '请填写密码']);
    
        // 验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码失效']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        $update = [
            'user_update_time' => date('Y-m-d H:i:s'),
            'user_password' => md5($pwd)
        ];
        $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        if($res){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            return json(['status' => 200, 'msg' => '重置成功']);
        }else{
            return json(['status' => 0, 'msg' => '重置失败']);
        }
    }
    
    /**
     * 意见反馈
     * @return \think\response\Json
     * feedback_content    反馈内容
     * feedback_phone    联系方式
     */
    public function feedback(){
        $content = input('feedback_content');
        $phone = trim(input('feedback_phone'), ' ');
        $feedback = [
            'f_user_id' => $this->user_id,
            'feedback_phone' => $phone,
            'feedback_content' => $content,
            'feedback_time' => $this->create_time
        ];
        $res = Db::table('up_feedback')->insert($feedback);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功，我们会尽快处理']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 帮助中心列表
     * @return \think\response\Json
     */
    public function helpCenter(){
        $list = Db::table('up_help')
            ->where('help_status', 1)
            ->field('help_id, help_title, help_content')
            ->order('help_sort asc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 帮助中心内容
     * @return \think\response\Json
     * help_id   ID
     */
    public function helpDetail(){
        $help_id = input('help_id');
        $help = Db::table('up_help')->where('help_id', $help_id)->value('help_content');
        return json(['status' => 200, 'msg' => $help]);
    }
    
    /**
     * 关于我们
     * @return \think\response\Json
     */
    public function aboutUs(){
        $us = Db::table('up_other')->where('other_id', 3)->value('other_aboutus');
        return json(['status' => 200, 'msg' => $us]);
    }
    
    /**
     * 报事报修列表
     * @return \think\response\Json
     */
    public function repairList(){
        $status = input('status'); // 0 全部   1 待处理  2 处理中   3 处理完成
        $xq_id = Db::table('up_xq_wy_user')->where('xw_user_id', $this->user_id)->value('xw_xq_id');
        $xq_name = Db::table('up_xq')->where('xq_id', $xq_id)->value('xq_name');
        $list = $this->myRepair(1, $status);
        if($list){
            foreach($list as $k => $v){
                $list[$k]['repair_time'] = date('Y-m-d H:i', strtotime($v['repair_time']));
                if(!empty($v['repair_do_time'])){
                    $list[$k]['repair_do_time'] = date('Y-m-d H:i', strtotime($v['repair_do_time']));
                }
                if(!empty($v['repair_over_time'])){
                    $list[$k]['repair_over_time'] = date('Y-m-d H:i', strtotime($v['repair_over_time']));
                }
            }
            return json(['status' => 200, 'msg' => $list, 'xq_name' => $xq_name]);
        }else{
            return json(['status' => 200, 'msg' => [], 'xq_name' => $xq_name]);
        }
    }
    
    /**
     * 处理报事报修
     * @return \think\response\Json
     * status     处理环节   2 接受   3 完成
     * repair_id    报事报修ID
     */
    public function doRepair(){
        $status = input('status');  // 2 接受   3 完成
        $repair_id = input('repair_id');
        $update['repair_status'] = $status;
        if($status == 2){
            $update['repair_done_user_id'] = $this->user_id;
            $update['repair_do_time'] = $this->create_time;
        }else{
            $update['repair_over_time'] = $this->create_time;
        }
        $res = Db::table('up_repair')->where('repair_id', $repair_id)->update($update);
        if($res){
            $repair = Db::table('up_repair')->where('repair_id', $repair_id)
                ->field('repair_user_id, repair_plot_id, repair_type_id')
                ->find();
            
            if($status == 2){
                $n_status = 2;
//                $str = '您的上报已被受理';
                $code=2;
                $str = array('msg'=>"您的上报已被受理",'type'=>5,'n_status'=>$n_status,'repair_id'=>$repair_id);
                $str1 = "您的上报已被受理";
            }else{
                $n_status = 3;
                $code=2;

//                $str = '您的上报已处理完成';
                $str = array('msg'=>"您的上报已处理完成",'type'=>5,'n_status'=>$n_status,'repair_id'=>$repair_id);
                $str1 = "您的上报已处理完成";
            }
            
            // 小区物业管理员
            $wy_user_ids = Db::table('up_xq_wy_user')->where('xw_xq_id', $repair['repair_plot_id'])->column('xw_user_id');
            // 保修类型
            $type_name = Db::table('up_repair_type')->where('rt_id', $repair['repair_type_id'])->value('rt_name');
            $show_user_id = ',' . $repair['repair_user_id']  . ',' . implode(',', $wy_user_ids) . ',';
            $notice = [
                'n_title' => $type_name,
                'n_type' => 2,
                'n_read' => ',0,',
                'n_delete' => ',0,',
                'n_time' => $this->create_time,
                'n_classify' => 1,
                'n_status' => $n_status,
                'n_where_id' => $repair_id,
                'n_user_id' => $show_user_id
            ];
            //                if($wy_user_ids){
//                    $show_user_id = ',' . $this->user_id . implode(',', $wy_user_ids) . ',';
//                }else{
//                    $show_user_id = ',' . $this->user_id . ',';
//                }
//                $notice['n_user_id'] = $show_user_id;
            Db::table('up_notice')->insert($notice);
            // 推送
            try{
                $list = [];
                array_push($list, (string)$repair['repair_user_id']);
//                $this->Jpush(1, $list, $str, ['send_type' => 1]);
                $this->sendJpush(1, $list, $str1, $str,$code);
            }catch(\Exception $e){
        
            }
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 报事报修详情
     * @return \think\response\Json
     * repair_id   报事报修ID
     * notice_id   消息ID
     */
    public function repairDetail(){
        $repair_id = input('repair_id');
        $notice_id = input('notice_id');
        if(isset($notice_id) && $notice_id != ''){
            $n_read = Db::table('up_notice')->where('n_id', $notice_id)->value('n_read');
            if(strpos($n_read, ',' . $this->user_id . ',') === false){
                $users = $n_read . $this->user_id . ',';
                Db::table('up_notice')->where('n_id', $notice_id)->setField('n_read', $users);
            }
        }
        $repair = Db::table('up_repair')
            ->join('up_repair_type', 'rt_id = repair_type_id', 'left')
            ->join('up_repair_comment', 'rc_repair_id = repair_id', 'left')
            ->field('rt_name, repair_time, repair_do_time, repair_over_time, repair_explain, repair_img, 
            repair_phone, repair_status, repair_addr, rc_content')
            ->where('repair_id', $repair_id)
            ->find();
        if(empty($repair['repair_img'])){
           $imgs = [];
        }else{
            $imgs = explode(',', trim($repair['repair_img'], ','));
        }
        $repair['repair_img'] = $imgs;
        return json(['status' => 200, 'msg' => $repair]);
    }
    
    /**
     * 评价
     * @return \think\response\Json
     * repair_id      上报ID
     * comment_content    评价内容
     * comment_type    评价标签  1 满意  2 一般  3 不满意
     */
    public function repairComment(){
        $repair_id = input('repair_id');
        $repair_comment = input('comment_content');
        $comment_type = input('comment_type'); // 1 满意   2 一般  3 不满意
        $insert = [
            'rc_repair_id' => $repair_id,
            'rc_type' => $comment_type,
            'rc_user_id' => $this->user_id,
            'rc_content' => $repair_comment,
            'rc_time' => $this->create_time
        ];
        $res = Db::table('up_repair_comment')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '评价成功']);
        }else{
            return json(['status' => 0, 'msg' => '隐藏失败']);
        }
    }
    
    /**
     * 报事报修类型
     * @return \think\response\Json
     */
    public function repairType(){
        $list = Db::table('up_repair_type')
            ->where('rt_status', 1)
            ->field('rt_id, rt_name')
            ->order('rt_time desc')
            ->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 提交报事报修
     * @return \think\response\Json
     * xq_id      小区ID
     * type_id    分类ID
     * repair_addr    上报位置
     * repair_explain    上报说明
     * repair_img        上报的图片
     * repair_phone      联系方式
     */
    public function appearRepair(){
        $repair_plot_id = input('xq_id');
        $repair_type_id = input('type_id');
        $repair_addr = input('repair_addr');
        $repair_explain = input('repair_explain');
        $repair_img = input('repair_img');
        $repair_phone = input('repair_phone');
        $insert = [
            'repair_user_id' => $this->user_id,
            'repair_plot_id' => $repair_plot_id,
            'repair_type_id' => $repair_type_id,
            'repair_addr' => $repair_addr,
            'repair_explain' => $repair_explain,
            'repair_img' => $repair_img,
            'repair_phone' => $repair_phone,
            'repair_time' => $this->create_time
        ];
        
        // 小区物业管理员
        $wy_user_ids = Db::table('up_xq_wy_user')->where('xw_xq_id', $repair_plot_id)->column('xw_user_id');
        // 保修类型
        $type_name = Db::table('up_repair_type')->where('rt_id', $repair_type_id)->value('rt_name');
        $show_user_id = ',' . implode(',', $wy_user_ids) . ',';
        $notice = [
            'n_title' => $type_name,
            'n_type' => 2,
            'n_read' => ',0,',
            'n_delete' => ',0,',
            'n_time' => $this->create_time,
            'n_classify' => 1,
            'n_status' => 1,
            'n_user_id' => $show_user_id
        ];
//        if($wy_user_ids){
//            $show_user_id = ',' . $this->user_id . implode(',', $wy_user_ids) . ',';
//        }else{
//            $show_user_id = ',' . $this->user_id . ',';
//        }
//        $notice['n_user_id'] = $show_user_id;
        
        Db::startTrans();
        try{
            $id = Db::table('up_repair')->insertGetId($insert);
            $notice['n_where_id'] = $id;
            
            Db::table('up_notice')->insert($notice);
            Db::commit();
            return json(['status' => 200, 'msg' => '提交成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 我的房产
     * @return \think\response\Json
     * sh_status    审核状态    1 审核中   2 已认证   3 认证失败
     */
    public function myXq($sh_status){
        if($sh_status == 1){
            $order = 'xq_apply_time desc';
        }else{
            $order = 'xq_pass_time desc';
        }
        $xq = Db::table('up_xq_property')
            ->alias('a')
            ->join('up_xq ux', 'ux.xq_id = a.xq_id', 'left')
            ->join('up_xq_block', 'xq_block_id = block_id', 'left')
            ->join('up_xq_unit', 'unit_id = xq_unit_id', 'left')
            ->join('up_xq_room', 'xq_room_id = room_id', 'left')
            ->field('xq_name, xq_address, a.xq_id, xq_nopass_reason, xq_sh_status, block_name, 
            unit_name, room_name')
            ->where('xq_user_id', $this->user_id)
            ->where('xq_sh_status', $sh_status)
            ->order($order)
            ->select();
        if($xq){
            return json(['status' => 200, 'msg' => $xq]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 清空审核失败记录
     * @return \think\response\Json
     */
    public function clearMyXq(){
        $res = Db::table('up_xq_property')
            ->where('xq_user_id', $this->user_id)
            ->where('xq_sh_status', 3)
            ->delete();
        if($res){
            return json(['status' => 200, 'msg' => '清空成功']);
        }else{
            return json(['status' => 0, 'msg' => '清空失败']);
        }
    }
    
    /**
     * 提交租户认证
     * @return \think\response\Json
     * xq_id     小区ID
     * xq_block_id   幢
     * xq_unit_id    单元
     * xq_room_id    房号
     * tenant_phone   租户手机号
     * tenant_name    租户姓名
     * tenant_code    租户身份证号
     */
    public function addXqLessee(){
        $xq_id = input('xq_id'); // 小区ID
        $xq_block_id = input('xq_block_id'); // 幢
        $xq_unit_id = input('xq_unit_id'); // 单元
        $xq_room_id = input('xq_room_id'); // 房号
        $tenant_phone = input('tenant_phone'); // 租户手机号
        $tenant_name = input('tenant_name'); // 租户姓名
        $tenant_code = input('tenant_code'); // 租户身份证号
        // 是否申请过房主角色
        $exit_role = Db::table('up_xq_property')
            ->where('xq_role', 2)
            ->where('xq_sh_status',2)
            ->where('xq_user_id', $this->user_id)
            ->find();
        if($exit_role){
            return json(['status' => 0, 'msg' => '一个用户只能申请一个角色']);
        }
        // 是否已经提交过
//        $exit = Db::table('up_xq_property')
//            ->where('xq_role', 1)
//            ->where('xq_user_id', $this->user_id)
//            ->find();
//        if($exit){
//            return json(['status' => 0, 'msg' => '租户只能承租一套房产']);
//        }
        $property = [
            'xq_user_id' => $this->user_id,
            'xq_id' => $xq_id,
            'xq_block_id' => $xq_block_id,
            'xq_unit_id' => $xq_unit_id,
            'xq_room_id' => $xq_room_id,
            'xq_role' => 1,
            'xq_apply_time' => $this->create_time,
            'tenant_phone' => $tenant_phone,
            'tenant_name' => $tenant_name,
            'tenant_code' => $tenant_code
        ];
        // 自动审核
        $have_house = Db::table('up_xq_house')
            ->where('xq_id', $xq_id)
            ->where('block_id', $xq_block_id)
            ->where('unit_id', $xq_unit_id)
            ->where('room_id', $xq_room_id)
            ->find();
        // 认证状态
        if($have_house){
            $property['xq_house_num'] = $have_house['house_num'];
            $property['xq_own_phone'] = $have_house['house_my_phone'];
            $property['xq_own_name'] = $have_house['house_my_name'];
            $property['xq_own_code'] = $have_house['house_my_card'];
            $property['xq_sh_status'] = 2;
            $property['xq_pass_time'] = $this->create_time;
            $property['house_id'] = $have_house['house_id'];
            // 自动审核，修改用户角色
            $user_role = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_role');

            $list = [];
            $v = (string)$this->user_id;
            array_push($list, $v);
            $code=2;
            $str = array('msg'=>"您的租户认证已通过",'type'=>3);
            $str1 = "您的租户认证已通过";
            try{
                $this->sendJpush(1, $list, $str1, $str,$code);
            }catch(\Exception $e){

            }
            if($user_role != 1){
                Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_role', 1);
            }
        }else{
            $property['xq_sh_status'] = 1;
        }
        $res = Db::table('up_xq_property')->insert($property);
        if($res){
            return json(['status' => 200, 'house_id'=>$have_house['house_id'],'msg' => '提交成功，我们会尽快处理', 'role' => 1, 'xq_sh_status' => $property['xq_sh_status']]);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 提交房主认证
     * @return \think\response\Json
     * xq_id      小区ID
     * xq_block_id     幢
     * xq_unit_id      单元
     * xq_room_id      房号
     * xq_own_phone    房屋拥有者手机号
     * xq_own_name     房屋拥有者姓名
     * xq_own_code     房屋拥有者身份证号
     */
    public function addXqOwner(){
        $xq_id = input('xq_id'); // 小区ID
        $xq_block_id = input('xq_block_id'); // 幢
        $xq_unit_id = input('xq_unit_id'); // 单元
        $xq_room_id = input('xq_room_id'); // 房号
        $xq_own_phone = input('xq_own_phone'); // 房屋拥有者手机号
        $xq_own_name = input('xq_own_name'); // 房屋拥有者姓名
        $xq_own_code = input('xq_own_code'); // 房屋拥有者身份证号

        // 是否申请过租户角色
        $exit_role = Db::table('up_xq_property')
            ->where('xq_role', 1)
            ->where('xq_sh_status',2)
            ->where('xq_user_id', $this->user_id)
            ->find();
        if($exit_role){
            return json(['status' => 0, 'msg' => '一个用户只能申请一个角色']);
        }
        
        // 是否已经提交过申请
        $exit = Db::table('up_xq_property')
//            ->where('xq_user_id', $this->user_id)
            ->where('xq_id', $xq_id)
            ->where('xq_block_id', $xq_block_id)
            ->where('xq_unit_id', $xq_unit_id)
            ->where('xq_room_id', $xq_room_id)
            ->where('xq_role', 2)
            ->where('xq_sh_status','IN',array(1,2))
            ->find();
        if($exit){
            return json(['status' => 0, 'msg' => '该房产已有人提交']);
        }
        
        $house = [
            'xq_user_id' => $this->user_id,
            'xq_id' => $xq_id,
            'xq_block_id' => $xq_block_id,
            'xq_unit_id' => $xq_unit_id,
            'xq_room_id' => $xq_room_id,
            'xq_own_phone' => $xq_own_phone,
            'xq_own_name' => $xq_own_name,
            'xq_own_code' => $xq_own_code,
            'xq_role' => 2,
            'xq_apply_time' => $this->create_time
        ];

//        $have = Db::table('up_xq_house')
//            ->where('xq_id', $xq_own_phone)
//            ->where('block_id', $xq_own_name)
//            ->where('unit_id', $xq_unit_id)
//            ->where('room_id', $xq_room_id)
//            ->find();
                $have = Db::table('up_xq_house')
            ->where('house_my_phone', $xq_own_phone)
            ->where('house_my_name', $xq_own_name)
            ->where('house_my_card', $xq_own_code)
            ->find();
        if($have){
//            if($have['is_bind'] == 1 && $have['house_bind_user'] == $this->user_id){
                $house['xq_sh_status'] = 2;
//            }else{
//                $house['xq_sh_status'] = 1;
//            }
            
            $house['xq_pass_time'] = $this->create_time;
            $house['house_id'] = $have['house_id'];
            $house['xq_house_num'] = $have['house_num'];
            // 自动审核，修改用户角色
            $user_role = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_role');
            if($user_role != 2){
                Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_role', 2);
            }
        }else{
            $house['xq_sh_status'] = 1;
        }
        if($house['xq_sh_status'] == 2){
            $list = [];
            $v = (string)$this->user_id;
            array_push($list, $v);
            $code=2;
            $str = array('msg'=>"您的房主认证已通过",'type'=>4);
            $str1 = "您的房主认证已通过";
            try{
                $this->sendJpush(1, $list, $str1, $str,$code);
            }catch(\Exception $e){

            }
        }
        $res = Db::table('up_xq_property')->insert($house);
        if($res){
            return json(['status' => 200, 'house_id'=>$have['house_id'],'msg' => '提交成功，我们会尽快处理', 'role' => 2, 'xq_sh_status' => $house['xq_sh_status']]);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 根据小区选楼栋
     * @param $xq_id    小区ID
     * @return \think\response\Json
     */
    public function getBlockByXq($xq_id){
        $list = Db::table('up_xq_block')
            ->field('block_id, block_name')
            ->where('xq_id', $xq_id)
            ->where('is_del', 1)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 根据楼栋选单元
     * @param $block_id     楼栋ID
     * @return \think\response\Json
     */
    public function getUnitByBlock($block_id){
        $list = Db::table('up_xq_unit')
            ->field('unit_id, unit_name')
            ->where('blonk_id', $block_id)
            ->where('is_del', 1)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 根据单元选房号
     * @param $unit_id    单元ID
     * @return \think\response\Json
     */
    public function getRoomByUnit($unit_id){
        $list = Db::table('up_xq_room')
            ->field('room_id, room_name')
            ->where('unit_id', $unit_id)
            ->where('is_del', 1)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的留言
     * @param $range   1 列表   2 统计
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function myMessage($range){
        $user_topic = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_topic');
        $data = Db::table('up_topic_message')
            ->alias('a')
            ->join('up_topic ut', 'ut.topic_id = a.topic_id', 'left')
            ->where('topic_status', 1)
            ->where('topic_message_user', $this->user_id);
//        if($user_topic == 1){ // 不是话题管理员只能看到自己的
//            $data->where('topic_message_user', $this->user_id);
//        }
        if($range == 1){ // 列表
            $data = $data
                ->field('topic_message_id, a.topic_id, topic_message_content, topic_message_time, 
            topic_title, topic_user_id')
                ->page($this->page, 20)
                ->order('topic_message_time desc')
                ->select();
        }else{ // 数量
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 我的话题
     * @param $range   1 列表    2 统计
     * @return false|int|\PDOStatement|string|\think\Collection|\think\db\Query
     */
    private function myTopic($range){
        $user_topic = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_topic');
        $data = Db::table('up_topic')
            ->where('topic_is_show', 1)
            ->where('topic_user_id', $this->user_id);
//        if($user_topic == 1){ // 不是话题管理员只能看到自己的
//            $data->where('topic_user_id', $this->user_id);
//        }
        if($range == 1){ // 列表
            $data = $data
                ->field('topic_id, topic_title, topic_img, topic_content, topic_fb_time')
                ->page($this->page, 20)
                ->order('topic_fb_time desc')
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 我的收藏
     * @param $range    1 列表    2 统计
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function myCollect($range){
        $data = Db::table('up_topic_like')
            ->alias('a')
            ->join('up_topic ut', 'ut.topic_id = a.topic_id', 'left')
            ->join('up_u_user', 'topic_user_id = user_id', 'left')
            ->where('topic_is_show', 1)
            ->where('topic_like_user', $this->user_id);
        if($range == 1){ // 列表
            $data = $data
                ->field('topic_like_id, ut.topic_id, topic_title, topic_img, topic_content, topic_fb_time, 
                user_username, user_image, topic_user_id')
                ->page($this->page, 20)
                ->order('topic_like_time desc')
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 我的报事报修
     * @param $range   1 列表   2 统计
     * @param int $status    0 全部   1 待处理   2 处理中   3 处理完
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function myRepair($range, $status = 0){
        $my_xq = Db::table('up_xq_wy_user')->where('xw_user_id', $this->user_id)->value('xw_xq_id');
        $data = Db::table('up_repair')
            ->join('up_repair_type', 'rt_id = repair_type_id', 'left');
        if($range == 1){ //列表
            $data->field('repair_id, rt_name, repair_addr, repair_explain, repair_time, repair_status, 
            repair_do_time, repair_over_time')
                ->order('repair_time desc')
                ->page($this->page, 10);
            if($status == 0){ // 全部
                 $data->where(function ($query) use ($my_xq){
                        $query->where('repair_plot_id', $my_xq)
                            ->where('repair_status', 1);
                    })->whereOr(function ($query1){
                        $query1->where('repair_done_user_id', $this->user_id)
                            ->where('repair_status', 'IN', '2,3');
                    });
            }elseif($status == 1){ // 待处理
                $data->where('repair_plot_id', $my_xq)
                    ->where('repair_status', 1);
            }else{
                $data->where('repair_done_user_id', $this->user_id)
//                    ->exp
                    ->where('repair_status', $status);
            }
            $data = $data->select();
        }else{
            $data = $data->where('repair_plot_id', $my_xq)->where('repair_status', 1)->count();
        }
        return $data;
    }
}